{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户注册</title>
    <link rel="stylesheet" href="{% static 'css/bootstrap.css' %}"/>
    <script src="{% static 'js/jquery-3.3.1.js' %}"></script>
    <script src="{% static 'js/bootstrap.js' %}"></script>
</head>
<body>
<div class="container">
    <div class="row" style="margin-top: 50px;">
        <div class="col-md-4"></div>
        <div class="col-md-4">
            <h4 class="text-center text-warning">用户注册</h4>
            <form>
                {% csrf_token %}
                <div class="form-group">
                    <label for="username">用户名:</label>
                    <input type="text" name="username" id="username" class="form-control">
                </div>
                <div class="form-group">
                    <label for="passwd">密码:</label>
                    <input type="password" name="passwd" id="passwd" class="form-control">
                </div>
                <div class="form-group">
                    <input type="button" id="submit" class="btn btn-primary" value="注册">
                </div>
                <p><a href="{% url 'user:login' %}">返回登录</a></p>
            </form>
        </div>
        <div class="col-md-4"></div>
    </div>
</div>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title" id="myModalLabel">注册提示</h4>
            </div>
            <div class="modal-body">注册成功</div>
            <div class="modal-footer">
                <a class="btn btn-primary" href="{% url 'user:login' %}">返回登陆页面</a>
            </div>
        </div>
    </div>
</div>
</body>
<script>
    $(function () {
        $('#submit').on('click', function () {
            var username = $('#username').val().trim();
            var passwd = $('#passwd').val().trim();
            if (username && passwd) {
                $.ajax({
                    url: "{% url 'user:register' %}",
                    method: 'post',
                    data: {
                        username: username,
                        password: passwd,
                        csrfmiddlewaretoken: $('[name=csrfmiddlewaretoken]').val()
                    },
                    success: function (data) {
                        console.log(data)
                        if (data.code == 0) {
                            $('#myModal').modal('show');
                        } else {
                            alert('注册失败')
                        }
                    }
                })
            }

        })
    })
</script>
</html>